package team.nmsg.ge.system.bean;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;
import org.apache.shiro.authc.AuthenticationToken;







import com.alibaba.fastjson.JSONObject;

import team.nmsg.ge.system.AppServerConstant;
import team.nmsg.ge.system.AppServerParams;
import team.nmsg.ge.system.exception.NMSTokenExpireException;
import team.nmsg.ge.system.interf.ISysBean;

public class AppToken implements ISysBean, AuthenticationToken{

	private static final long serialVersionUID = 1L;
	
	private String token;
	
	private long createTime;
	
	private long expireTime;
	private long refreshTime;
	
	private long uid;
	private String userName;
	

	private List<String> roles = new ArrayList<>();
	private List<String> opers = new ArrayList<>();
	
	
	public AppToken(){
		
	}
	
	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public long getCreateTime() {
		return createTime;
	}

	public void setCreateTime(long createTime) {
		this.createTime = createTime;
	}
	
	public long getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(long expireTime) {
		this.expireTime = expireTime;
	}

	public long getRefreshTime() {
		return refreshTime;
	}

	public void setRefreshTime(long refreshTime) {
		this.refreshTime = refreshTime;
	}
	
	public long getUid() {
		return uid;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}


	public List<String> getRoles() {
		return roles;
	}

	public void setRoles(List<String> roles) {
		this.roles = roles;
	}

	public List<String> getOpers() {
		return opers;
	}

	public void setOpers(List<String> opers) {
		this.opers = opers;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public String createClientToken(){
		JSONObject obj = new JSONObject();
		
		obj.put("token", token);
		obj.put("createTime", createTime);
		obj.put("expireTime", expireTime);
		obj.put("refreshTime", refreshTime);
		obj.put("uid", uid);
		obj.put("userName", userName);
		String jsonStr = obj.toString();
		try {
			byte[] encodeBase64 = Base64.encodeBase64( jsonStr.getBytes( AppServerConstant.UTF8 ) );
			return new String(encodeBase64);
		} catch ( Exception e) {
		}
		
		return jsonStr;
	}
	
	@Override
	public JSONObject toJson() {
		return null;
	}
	
	public static AppToken fromClientToken(String clientTokenStr) {
		String jsonStr = clientTokenStr;
		try {
			byte[] decodeBase64 = Base64.decodeBase64( clientTokenStr.getBytes( AppServerConstant.UTF8 ));
			jsonStr = new String( decodeBase64) ; 
		} catch (UnsupportedEncodingException e) {
		}
		
		JSONObject obj = JSONObject.parseObject(jsonStr);
		String tmpToken = obj.getString("token");
		Long tmpCreateTime = obj.getLong("createTime");
		Long tmpExpireTime = obj.getLong("expireTime");
		Long tmpRefreshTime = obj.getLong("refreshTime");
		Long tmpUid = obj.getLong("uid");
		String username = obj.getString("userName");
		
		AppToken token = new AppToken();
		token.setToken(tmpToken);
		token.setCreateTime(tmpCreateTime);
		token.setExpireTime(tmpExpireTime);
		token.setRefreshTime(tmpRefreshTime);
		token.setUid(tmpUid);
		token.setUserName(username);
		return token;
	}
	
	
	@Override
	public String toString() {
		return "AppToken [token=" + token + ", createTime=" + createTime
				+ ", expireTime=" + expireTime + ", refreshTime=" + refreshTime
				+ ", uid=" + uid + "]";
	}
	
	public static AppToken newToken(){

		return newToken( UUID.randomUUID().toString().replaceAll("-" , "") );
	}
	
	public static AppToken newToken( String tokenStr ){
		if( tokenStr == null ){
			tokenStr = UUID.randomUUID().toString().replaceAll("-" , "");
		}
		AppToken token = new AppToken();
		token.setToken(  tokenStr  );
		token.setCreateTime(System.currentTimeMillis() );
		token.setExpireTime( AppServerParams.TOKEN_EXPIRE_TIME );
		token.setRefreshTime( AppServerParams.TOKEN_REFRESH_TIME );
		return token;
	}

	public static void main(String[] args) throws NMSTokenExpireException {
		
		AppToken tokenObj = newToken();
		
		String createClientToken = tokenObj.createClientToken();
		System.out.println(  createClientToken  );
		
		AppToken fromClientToken = AppToken.fromClientToken(createClientToken);
		System.out.println(  tokenObj );
		System.out.println(  fromClientToken );
		
	}

	@Override
	public Object getPrincipal() {
		// TODO Auto-generated method stub
		return token;
	}

	@Override
	public Object getCredentials() {
		// TODO Auto-generated method stub
		return "";
	}
	
}
